<template>
  <div class="content-container">
    <el-row
      type="flex"
      justify="space-between"
      class="header-filter four-border-blueBg"
    >
      <div class="row-left">
        <TextInput
          v-model="listParams.tname"
          class="item"
          clearable
          :placeholder="$t('userCenter.orgName')"
        />
        <!-- <TextInput
          v-model="listParams.adminName"
          class="item"
          clearable
          :placeholder="$t('userCenter.adminName')"
        /> -->
        <DateTimeRange v-model="timePeriod" class="item" picker-type="2" />
        <el-button type="primary" @click="handleQuery">{{
          $t('public.search')
        }}</el-button>
        <el-button plain @click="handleReset">{{
          $t('public.reset')
        }}</el-button>
      </div>
      <div class="row-right">
        <el-button type="primary" @click="handleCreate"
          >+ {{ $t('public.create') }}</el-button
        >
      </div>
    </el-row>
    <div class="base-table-container">
      <BaseTable
        ref="refContentTable"
        v-loading="loading"
        :data="tableData"
        row-key="tid"
      >
        <TableColumn
          prop="tname"
          :label="$t('userCenter.orgName')"
          :column-width="columnWidthObj.tname"
        />
        <TableColumn
          prop="identifier"
          :label="$t('userCenter.orgId')"
          :column-width="columnWidthObj.identifier"
        />
        <!-- <TableColumn
          prop="status"
          :label="$t('public.status')"
          :column-width="columnWidthObj.status"
        >
          <template #default="{ row }">
            <span>{{
              g_options.getOptionText(
                g_options.organizeStatusOptions,
                row.status,
                '--'
              )
            }}</span>
          </template>
        </TableColumn> -->
        <TableColumn
          prop="createTime"
          :label="$t('userCenter.joinTime')"
          :column-width="columnWidthObj.createTime"
        />
        <TableColumn
          prop="action"
          fixed="right"
          :label="$t('public.action')"
          :column-width="columnWidthObj.action"
        >
          <template #default="{ row }">
            <el-button text @click="handleDetailDialog(row)">
              {{ $t('public.detail') }}
            </el-button>
            <el-button
              v-if="!row.systemTenant"
              text
              @click="handleEditDialog(row)"
            >
              {{ $t('userCenter.orgEdit') }}
            </el-button>
            <el-button v-if="!row.systemTenant" text @click="handleDelete(row)">
              {{ $t('public.delete') }}
            </el-button>
          </template>
        </TableColumn>
      </BaseTable>
      <BasePagination
        v-model:page-size="pageSize"
        v-model:current-page="pageNum"
        :total="total"
        @change="getTableList()"
      />
    </div>
    <!-- 新建 -->
    <AddDialog
      v-if="addVisible"
      :id="rowId"
      v-model="addVisible"
      @update="handleQuery"
    />
    <!-- 详情 -->
    <DetailDialog v-if="detailVisible" :id="rowId" v-model="detailVisible" />
    <!-- 编辑 -->
    <EditDialog
      v-if="editVisible"
      :id="rowId"
      v-model="editVisible"
      @update="handleQuery"
    />
  </div>
</template>

<script setup>
import AddDialog from './component/AddDialog.vue'
import EditDialog from './component/EditDialog.vue'
import DetailDialog from './component/DetailDialog.vue'

const listParams = ref({
  tname: '',
  // adminName: '',
  startTime: '',
  endTime: ''
})
const timePeriod = ref([])
const refContentTable = ref(null)
const loading = ref(false)
const tableData = ref([])
const columnWidthObj = ref({})
const total = ref(0)
const pageSize = ref(10)
const pageNum = ref(1)
const addVisible = ref(false)
const rowId = ref('')
const detailVisible = ref(false)
const editVisible = ref(false)

const getTableList = () => {
  loading.value = true
  tableData.value = []
  g_api.userCenter
    .apiTenantList({
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      ...listParams.value,
      startTime: timePeriod.value[0] || '',
      endTime: timePeriod.value[1] || ''
    })
    .then(({ code, data }) => {
      if (code === '200' && data) {
        tableData.value = data.items
        total.value = data.total
        useTableColumnWidth({
          refTable: refContentTable.value
        }).getTableColumnWidth(data => {
          columnWidthObj.value = data
        })
      }
    })
    .finally(() => {
      loading.value = false
    })
}

onMounted(() => {
  getTableList()
})

const handleQuery = () => {
  pageNum.value = 1
  getTableList()
}

const handleReset = () => {
  Object.assign(listParams.value, {
    tname: '',
    // adminName: '',
    startTime: '',
    endTime: ''
  })
  timePeriod.value = []
  handleQuery()
}

const handleCreate = () => {
  addVisible.value = true
}

const handleDetailDialog = row => {
  detailVisible.value = true
  rowId.value = row.tid
}

const handleEditDialog = row => {
  editVisible.value = true
  rowId.value = row.tid
}

const handleDelete = row => {
  g_utils
    .deleteConfirm()
    .then(() => {
      loading.value = true
      g_api.userCenter
        .apiTenantDelete({
          tid: row.tid
        })
        .then(res => {
          if (res.code === '200') {
            ElMessage.success(res.message)
            handleQuery()
          }
        })
        .finally(() => {
          loading.value = false
        })
    })
    .catch(() => {})
}
</script>

<style lang="scss" scoped></style>
